package com.campus.service.impl;

import com.alibaba.fastjson2.JSON;
import com.campus.common.CampusWall;
import com.campus.common.LostItemCode;
import com.campus.common.Result;
import com.campus.entity.LostItems;
import com.campus.entity.Users;
import com.campus.mapper.LostItemsMapper;
import com.campus.service.LostItemService;
import com.campus.utils.SnowFlakeUtil;
import com.campus.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import java.util.Set;

@Service
public class LostItemServiceImpl implements LostItemService {
    @Autowired
    private LostItemsMapper lostItemsMapper;
    @Autowired
    private TokenUtils tokenUtils;
    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 发布失物招领帖子
     *
     * @param lostItems
     * @param request
     * @return
     */
    @Override
    public Result createLostItem(LostItems lostItems, HttpServletRequest request) {
        //生成主键id
        lostItems.setItemId(SnowFlakeUtil.getId().toString());
        //获取登陆的用户及发布帖子的用户
        String token = tokenUtils.getToken(request.getHeader(CampusWall.CAMPUS_TOKEN));
        Users currentUser = tokenUtils.getCurrentUser(token);
        lostItems.setUserId(currentUser.getUserId());
        lostItems.setStatus(LostItemCode.OPEN_STATUS);
        lostItems.setJsonImages(JSON.toJSONString(lostItems.getImages()));
        Set<String> keys = redisTemplate.keys("LostItems:*");
        redisTemplate.delete(keys);
        int i = lostItemsMapper.insertLostItems(lostItems);
        if (i > 0) {
//            Set<String> keys = redisTemplate.keys("LostItems:*");
            redisTemplate.delete(keys);
            return Result.ok("发布成功");
        }
        return Result.err(Result.CODE_500, "发布失败");
    }

    /**
     * 根据条件查询失物招领的东西
     *
     * @param page
     * @param size
     * @param type
     * @param status
     * @param keyword
     * @param request
     * @return
     */
    @Override
    public Result getItemsList(int page, int size, String type, String status, String keyword, HttpServletRequest request) {
        PageHelper.startPage(page, size);
        PageInfo<LostItems> pageInfo = null;
        String redisKey = getLostItemKeys(size, type, status, keyword);
        String redisStr = redisTemplate.opsForValue().get(redisKey);
        if (StringUtils.hasText(redisStr)) {
            pageInfo = JSON.parseObject(redisStr, PageInfo.class);
            return Result.ok(pageInfo);
        }
        List<LostItems> lostItems = lostItemsMapper.selectAllLostItemsByInfo(type, status, keyword);
        pageInfo = new PageInfo<LostItems>(lostItems);
        redisTemplate.opsForValue().set(redisKey, JSON.toJSONString(pageInfo));
        PageHelper.clearPage();
        return Result.ok(pageInfo);
    }

    /**
     * 获取失物招领redis key
     */
    public String getLostItemKeys(int size, String type, String status, String keyword) {
        return "LostItems:" + type + status + keyword + status + type;
    }
}
